#include "apue.h"
#include <errno.h>
#include <unistd.h>

/**
 * system函数,没对信号进行处理
 */
int 
system2(const char *cmdtring) {
	pid_t pid;
	int status;

	if (cmdtring == NULL) {
		return (1);
	}

	if ((pid = fork()) < 0) {
		status = -1;
	} else if (pid == 0) {
		execl("/bin/sh", "sh", "-c", cmdtring, (char *)0);
		_exit(127);
	} else {
		while (waitpid(pid , &status, 0) < 0) {
			if (errno != EINTR) {
				status = -1;
				break;
			}
		}
	}

	return (status);
}

int 
main(void) {
	int status;

	if ((status = system("date")) < 0) {
		err_sys("system() error");
	}

	pr_exit(status);

	if ((status = system("nocmdexist")) < 0) {
		err_sys("system() error");
	}

	pr_exit(status);

	if ((status = system("who; exit 44")) < 0) {
		err_sys("system() error");
	}

	pr_exit(status);

	if ((status = system2("ls")) < 0) {
		err_sys("system() error");
	}

	pr_exit(status);

	exit(0);
}
